﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Remote_Desktop_Administrator
{
    public partial class ConnectionForm : Form
    {
        TreeView tvm = new TreeView();
        bool blnUpdateConnection = false;
        int intExistingConnection;

        //
        // Overview:
        // Default constructor
        //
        public ConnectionForm()
        {
            InitializeComponent();
        }

        //
        // Overview:
        // Closes form
        //
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //
        // Overview:
        // Calls a combo box refresh when form loads so folder array contents are shown
        //
        private void NewConnectionForm_Load(object sender, EventArgs e)
        {
            Connection.RefreshCboFolders(cboFolders);
        }

        //
        // Overview:
        // Saves connection info, closes form, calls refresh of treeview
        //
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtConnectionName.Text == "" || txtServerName.Text == "")
            {
                MessageBox.Show("You must enter at least a server name and a connection name.");
            }
            else
            {

                string strServer, strConnName, strUser, strPass, strDom, strNote, strColor, strFolder, strRes, strPort;
                bool blnPrinters, blnDrives, blnClip;

                try
                {
                    strServer = txtServerName.Text;
                    strConnName = txtConnectionName.Text;
                    strUser = txtUserName.Text;
                    strPass = txtPassword.Text;
                    strDom = txtDomain.Text;
                    blnPrinters = chkRedirectPrinters.Checked;
                    blnDrives = chkRedirectDrives.Checked;
                    blnClip = chkRedirectClipboard.Checked;
                    strNote = txtNote.Text;
                    strColor = cboColor.SelectedItem.ToString();
                    strFolder = cboFolders.Text;
                    strRes = cboResolution.SelectedItem.ToString();
                    strPort = txtPort.Text;

                    // bool is flag for creating a new or updating an existing...controlled via public property
                    if (!blnUpdateConnection)
                    {
                        Connection.SaveNewConnection(strServer, strConnName, strUser, strPass, strDom, blnPrinters, blnDrives, blnClip, strNote, strColor, strFolder, strRes, strPort);
                        MessageBox.Show("Connection added successfully.", "Results", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        Connection.SaveConnectionChanges(intExistingConnection, strServer, strConnName, strUser, strPass, strDom, blnPrinters, blnDrives, blnClip, strNote, strColor, strFolder, strRes, strPort);
                        MessageBox.Show("Connection updated successfully.", "Results", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error while adding connection - " + ex.Message);
                }
                finally
                {
                    Connection.RefreshTreeView(tvm);
                    this.Close();
                }
            }
        }
        
        //
        // Overview:
        // Takes reference for treeview from main form and sets it to a class-level variable (ref to orginal treeview)
        // 
        public void ReceiveTreeViewRef(TreeView treeViewMain)
        {
            tvm = treeViewMain;
        }

        //
        // Overview:
        // Button click launches the add folder form
        // 
        private void btnNewFolder_Click(object sender, EventArgs e)
       {
           FolderForm frm1 = new FolderForm();
           frm1.ReceiveCboBox(cboFolders);
           frm1.ShowDialog();
       }
        
        //
        // Overview:
        // The next few methods expose properties to other objects while form is active
        // 
        public string prop_ServerName
        {
            get { return txtServerName.Text; }
            set { txtServerName.Text = value; }
        }
        public string prop_ConnectionName
        {
            get { return txtConnectionName.Text; }
            set { txtConnectionName.Text = value; }
        }
        public string prop_UserName
        {
            get { return txtUserName.Text; }
            set { txtUserName.Text = value; }
        }
        public string prop_Password
        {
            get { return txtPassword.Text; }
            set { txtPassword.Text = value; }
        }
        public string prop_Domain
        {
            get { return txtDomain.Text; }
            set { txtDomain.Text = value; }
        }
        public string prop_RedirectPrinters
        {
            get { return chkRedirectPrinters.Checked.ToString(); }
            set { chkRedirectPrinters.Checked = Convert.ToBoolean(value); }
        }
        public string prop_RedirectDrives
        {
            get { return chkRedirectDrives.Checked.ToString(); }
            set { chkRedirectDrives.Checked = Convert.ToBoolean(value); }
        }
        public string prop_RedirectClipboard
        {
            get { return chkRedirectClipboard.Checked.ToString(); }
            set { chkRedirectClipboard.Checked = Convert.ToBoolean(value); }
        }
        public string prop_Note
        {
            get { return txtNote.Text; }
            set { txtNote.Text = value; }
        }
        public bool prop_UpdateConnection
        {
            get { return blnUpdateConnection; }
            set { blnUpdateConnection = value; }
        }
        public int prop_ExistingConnectionIndex
        {
            get { return intExistingConnection; }
            set { intExistingConnection = value; }
        }
        public string prop_Color
        {
            get { return cboColor.SelectedItem.ToString(); }
            set { cboColor.Text = value; }
        }
        public string prop_Folder
        {
            get { return cboFolders.SelectedItem.ToString(); }
            set { cboFolders.Text = value; }
        }
        public string prop_Res
        {
            get { return cboResolution.SelectedItem.ToString(); }
            set { cboResolution.Text = value; }
        }

        public string prop_Port
        {
            get { return txtPort.Text; }
            set { txtPort.Text = value; }
        }

        private void txtServerName_Leave(object sender, EventArgs e)
        {
            txtConnectionName.Text = txtServerName.Text;
        }
    }
}
